{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d89ae951",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/chat_engine/chat_engine_best.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f59565a0-62c0-4048-8a7c-e60fba161cd2",
   "metadata": {},
   "source": [
    "# Chat Engine - Best Mode"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "433ea1f0-86e8-4912-8db8-dfe3d6a00b6d",
   "metadata": {},
   "source": [
    "The default chat engine mode is \"best\", which uses the \"openai\" mode if you are using an OpenAI model that supports the latest function calling API, otherwise uses the \"react\" mode"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a29935d",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97eea6c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-anthropic\n",
    "%pip install llama-index-llms-openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6733b9ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "38a1e5ab",
   "metadata": {},
   "source": [
    "## Download Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4950aa4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2024-01-27 12:15:55--  https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt\n",
      "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8001::154, 2606:50c0:8002::154, 2606:50c0:8003::154, ...\n",
      "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8001::154|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 75042 (73K) [text/plain]\n",
      "Saving to: ‘data/paul_graham/paul_graham_essay.txt’\n",
      "\n",
      "data/paul_graham/pa 100%[===================>]  73.28K  --.-KB/s    in 0.008s  \n",
      "\n",
      "2024-01-27 12:15:55 (9.38 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!mkdir -p 'data/paul_graham/'\n",
    "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b036854-2947-4ce9-b662-a17494f659b0",
   "metadata": {},
   "source": [
    "### Get started in 5 lines of code"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "600cf600-c0d4-4f15-9424-0a2b07c34e77",
   "metadata": {},
   "source": [
    "Load data and build index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e633c998-2d63-41c5-a678-46d7683e324a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import VectorStoreIndex, SimpleDirectoryReader\n",
    "from llama_index.llms.openai import OpenAI\n",
    "from llama_index.llms.anthropic import Anthropic\n",
    "\n",
    "llm = OpenAI(model=\"gpt-4\")\n",
    "data = SimpleDirectoryReader(input_dir=\"./data/paul_graham/\").load_data()\n",
    "index = VectorStoreIndex.from_documents(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bba0968-611f-465b-b98e-73eb8a5bc4e4",
   "metadata": {},
   "source": [
    "Configure chat engine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "938190e1-bb7c-4dda-895e-f8351026f8d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_engine = index.as_chat_engine(chat_mode=\"best\", llm=llm, verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b13bb12d-b583-4f35-bede-6d95a023a2fd",
   "metadata": {},
   "source": [
    "Chat with your data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "301220a3-0369-4104-bbbb-7bd084b793fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Added user message to memory: What are the first programs Paul Graham tried writing?\n",
      "=== Calling Function ===\n",
      "Calling function: query_engine_tool with args: {\n",
      "  \"input\": \"What are the first programs Paul Graham tried writing?\"\n",
      "}\n",
      "Got output: The first programs Paul Graham tried writing were on the IBM 1401 that their school district used for what was then called \"data processing.\" The language he used was an early version of Fortran.\n",
      "========================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "response = chat_engine.chat(\n",
    "    \"What are the first programs Paul Graham tried writing?\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50051bbf-ef40-4be0-bb74-b42e944a4c38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The first programs Paul Graham tried writing were on the IBM 1401. He used an early version of Fortran for these initial programs.\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
